# Oh My Posh 快速安装脚本
# 从 https://gitcode.com/Xuan_er123/oh-my-posh fork 仓库安装

Write-Host "=== Oh My Posh 快速安装脚本 ===" -ForegroundColor Green
Write-Host "从您的fork仓库安装: https://gitcode.com/Xuan_er123/oh-my-posh" -ForegroundColor Cyan

# 检查Git
try {
    git --version | Out-Null
    Write-Host "✓ Git已安装" -ForegroundColor Green
} catch {
    Write-Error "请先安装Git: winget install Git.Git"
    exit 1
}

# 检查Go
try {
    go version
    Write-Host "✓ Go已安装" -ForegroundColor Green
} catch {
    Write-Error "请先安装Go: winget install GoLang.Go"
    exit 1
}

# 设置Go代理
$env:GOPROXY = "https://goproxy.cn,direct"
Write-Host "✓ 已设置Go代理: $env:GOPROXY" -ForegroundColor Green

# 克隆仓库（如果不存在）
if (!(Test-Path "oh-my-posh")) {
    Write-Host "正在克隆仓库..." -ForegroundColor Yellow
    git clone https://gitcode.com/Xuan_er123/oh-my-posh.git
}

# 进入目录
Set-Location oh-my-posh

# 构建
Write-Host "正在构建oh-my-posh..." -ForegroundColor Yellow
Set-Location src

$buildDate = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
$version = "custom-$(Get-Date -Format 'yyyyMMdd-HHmm')"
$ldflags = "-s -w -X 'github.com/jandedobbeleer/oh-my-posh/src/build.Version=$version' -X 'github.com/jandedobbeleer/oh-my-posh/src/build.Date=$buildDate'"

go build -v -ldflags $ldflags -o oh-my-posh.exe

if (!(Test-Path "oh-my-posh.exe")) {
    Write-Error "构建失败"
    exit 1
}

Write-Host "✓ 构建成功!" -ForegroundColor Green

# 安装到本地
$installDir = "$env:LOCALAPPDATA\Programs\oh-my-posh"
Write-Host "正在安装到: $installDir" -ForegroundColor Yellow

New-Item -Path $installDir -ItemType Directory -Force | Out-Null
Copy-Item "oh-my-posh.exe" "$installDir\oh-my-posh.exe" -Force

# 添加到PATH
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($userPath -notlike "*$installDir*") {
    Write-Host "添加到PATH..." -ForegroundColor Yellow
    [Environment]::SetEnvironmentVariable("PATH", "$userPath;$installDir", "User")
    Write-Host "✓ 已添加到PATH" -ForegroundColor Green
} else {
    Write-Host "✓ PATH已包含安装目录" -ForegroundColor Green
}

# 测试安装
$testOutput = & "$installDir\oh-my-posh.exe" version 2>&1
Write-Host "✓ 安装成功! 版本: $testOutput" -ForegroundColor Green

Write-Host "`n=== 安装完成 ===" -ForegroundColor Green
Write-Host "现在您可以使用以下命令:" -ForegroundColor Cyan
Write-Host "1. 重启PowerShell" -ForegroundColor Yellow
Write-Host "2. 运行: oh-my-posh version" -ForegroundColor Yellow
Write-Host "3. 配置PowerShell: Add-Content `$PROFILE 'oh-my-posh init pwsh | Invoke-Expression'" -ForegroundColor Yellow

Write-Host "`n手动运行路径: $installDir\oh-my-posh.exe" -ForegroundColor Cyan
